package com.cskaoyan.controller;

import com.cskaoyan.bean.bo.front.order.OrderCommentBo;
import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketOrder;
import com.cskaoyan.bean.po.MarketOrderGoods;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.ordervo.FrontOrderInfoVo;
import com.cskaoyan.service.FrontOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Map;

/**
 * @ClassName:FrontOrderController
 * @Auther: HeXianZu
 * @Description:
 * @Date: 2022/10/13 22:11
 */
@RestController
@RequestMapping("wx/order")
public class FrontOrderController {

    @Autowired
    FrontOrderService frontOrderService;

    @RequestMapping("list")
    public BaseRespVo list(BaseParam baseParam, Integer showType) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String username = (String) session.getAttribute("username");
        Integer userId = frontOrderService.selectUserIdByUsername(username);

        CommonData data = null;
        // 查所有订单
        if (showType == 0) {
            data = frontOrderService.list(baseParam, showType, userId);
        }
        // 查寻未付款订单
        if (showType == 1) {
            data = frontOrderService.list(baseParam, showType, userId);
        }
        // 查询已付款订单
        if (showType == 2) {
            data = frontOrderService.list(baseParam, showType, userId);
        }
        // 查询已发货订单
        if (showType == 3) {
            data = frontOrderService.list(baseParam, showType, userId);
        }
        // 查询已收货订单
        if (showType == 4) {
            data = frontOrderService.list(baseParam, showType, userId);
        }
        return BaseRespVo.ok(data);
    }


    /**
     * 查看订单详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        FrontOrderInfoVo data = frontOrderService.detail(orderId);
        return BaseRespVo.ok(data);
    }

    /**
     * 申请退款
     *
     * @return
     */
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map) {
        int refund = frontOrderService.refund((Integer) map.get("orderId"));
        if (refund == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();
    }

    /**
     * 确认收货
     *
     * @param map
     * @return
     */
    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map) {
        int confirm = frontOrderService.confirm((Integer) map.get("orderId"));
        if (confirm == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        int delete = frontOrderService.delete((Integer) map.get("orderId"));
        if (delete == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();
    }

    /**
     * 评论商品信息回显
     */
    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId, Integer goodsId) {
        MarketOrderGoods goods = frontOrderService.goods(orderId, goodsId);
        return BaseRespVo.ok(goods);
    }

    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody OrderCommentBo orderCommentBo) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String username = (String) session.getAttribute("username");
        Integer userId = frontOrderService.selectUserIdByUsername(username);
        int comment = frontOrderService.comment(orderCommentBo, userId);
        if (comment == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map map) {
        Integer orderId = ((Integer) map.get("orderId"));
        int cancel = frontOrderService.cancel(orderId);
        if (cancel == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();
    }

    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map map) {
        Integer orderId = Integer.parseInt((String) map.get("orderId"));
        int prepay = frontOrderService.prepay(orderId);
        if (prepay == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();
    }


    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        List<Integer> goodsIds = (List<Integer>) session.getAttribute("goodsIds");
        MarketOrder marketOrder = (MarketOrder) session.getAttribute("marketOrder");
        List<Short> numbers = (List<Short>) session.getAttribute("numbers");
        List<String[]> specificationss = (List<String[]>) session.getAttribute("specificationss");
        int submit = frontOrderService.submit(map, marketOrder, goodsIds, numbers, specificationss);
        if (submit == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();
    }

}
